package com.iss.supermarket.OutFlowUI;

import com.iss.supermarket.util.List.ListModelArray;
import com.iss.supermarket.util.MessageWindow.MsgWin;
import com.iss.supermarket.util.entity.Goods;
import com.iss.supermarket.util.network.NetWork;
import com.iss.supermarket.util.repository.GoodsDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class OutFlowUI extends JFrame{
    //单例模式
    private static OutFlowUI OutFlowUI;
    private JLabel Title1,Title2;
    private JLabel[] OutFlowLabels;
    private JTextField[] OutFlowTextFields;
    private JButton jButton1,jButton2;
    private JPanel OutFlowPanel;
    private OutFlowList OutFlowList;

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:/spring_ioc.xml");
    GoodsDao goodsDao = ctx.getBean("GoodsDao", GoodsDao.class);

    public OutFlowUI(){
        NetWork.getNetWork(OutFlowUI.class.getSimpleName());
        InitAssembly();
        InitUI();
    }

    private void InitUI(){

        Box verticalBox1 = Box.createVerticalBox();
        Box verticalBox2 = Box.createVerticalBox();
        Box horizontalBox = Box.createHorizontalBox();
        JPanel rightPanel = new JPanel();
        JPanel leftPanel = new JPanel();

        JPanel titlepanel1 = new JPanel();
        titlepanel1.add(Title1);
        verticalBox1.add(titlepanel1);

        JPanel jPanel1 = new JPanel();
        jPanel1.add(OutFlowLabels[0]);
        jPanel1.add(OutFlowTextFields[0]);
        verticalBox1.add(jPanel1);

        JPanel jPanel2 = new JPanel();
        jPanel2.add(OutFlowLabels[1]);
        jPanel2.add(OutFlowTextFields[1]);
        verticalBox1.add(jPanel2);

        JPanel jPanel3 = new JPanel();
        jPanel3.add(OutFlowLabels[2]);
        jPanel3.add(OutFlowTextFields[2]);
        verticalBox1.add(jPanel3);

        JPanel jPanel4 = new JPanel();
        jPanel4.add(jButton2);
        jPanel4.add(jButton1);
        verticalBox1.add(jPanel4);
        leftPanel.add(verticalBox1);
        horizontalBox.add(leftPanel);

        JPanel titlepanel2 = new JPanel();
        titlepanel2.add(Title2);
        verticalBox2.add(titlepanel2);
        OutFlowList = new OutFlowList();
        verticalBox2.add(OutFlowList.getPanelPage());
        rightPanel.add(verticalBox2);
        horizontalBox.add(rightPanel);

        OutFlowPanel.add(horizontalBox);

//        container.add(verticalBox);
//        setSize(800,700);
//        setVisible(true);
    }

    private void InitAssembly(){
        Title1 = new JLabel("出库信息");
        Title1.setHorizontalAlignment(SwingConstants.CENTER);
        Title1.setFont(new Font("宋体",Font.BOLD, 20));
        Title2 = new JLabel("出库列表");
        Title2.setHorizontalAlignment(SwingConstants.CENTER);
        Title2.setFont(new Font("宋体",Font.BOLD, 20));

        OutFlowLabels = new JLabel[3];
        OutFlowLabels[0] = new JLabel("条形码：");
        OutFlowLabels[1] = new JLabel("数量：");
        OutFlowLabels[2] = new JLabel("商品名称：");

        for(int i=0;i<OutFlowLabels.length;++i){
            OutFlowLabels[i].setHorizontalAlignment(SwingConstants.LEFT);
            OutFlowLabels[i].setPreferredSize(new Dimension(100,30));
            OutFlowLabels[i].setFont(new Font("黑体",Font.PLAIN,16));
        }

        OutFlowTextFields = new JTextField[3];
        OutFlowTextFields[0] = new JTextField();
        //***测试***//
        OutFlowTextFields[0].setText("3913430656334");
        OutFlowTextFields[1] = new JTextField();
        OutFlowTextFields[2] = new JTextField();

        for(int i=0;i<OutFlowTextFields.length;++i){
            OutFlowTextFields[i].setHorizontalAlignment(SwingConstants.CENTER);
            OutFlowTextFields[i].setPreferredSize(new Dimension(250,25));
            OutFlowTextFields[i].setFont(new Font("黑体",Font.PLAIN,16));
        }

        OutFlowTextFields[2].setEnabled(false);

        jButton1 = new JButton("出库");
        jButton1.addActionListener(new OutFlowUI.ButtonAction());
        jButton1.setFont(new Font("黑体",Font.PLAIN,14));
        jButton2 = new JButton("确定");
        jButton2.addActionListener(new OutFlowUI.ButtonAction());
        jButton2.setFont(new Font("黑体",Font.PLAIN,14));

        OutFlowPanel = new JPanel();
        OutFlowPanel.setPreferredSize(new Dimension(800,700));
    }

    private class ButtonAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if("确定".equals(e.getActionCommand())){
                Goods goods = goodsDao.queryGoodsByBarCode(OutFlowTextFields[0].getText());
                //设置出库数量，默认出库数量为一
                if("".equals(OutFlowTextFields[1].getText())){
                    goods.setGdNum(1);
                }else{
                    goods.setGdNum(Integer.parseInt(OutFlowTextFields[1].getText()));
                }
                OutFlowTextFields[2].setText(goods.getGdBroadName());
                //刷新出库列表
                OutFlowList.flushList(goods);
            }else if("出库".equals(e.getActionCommand())){
                ListModelArray<Goods> listModelArray = OutFlowList.getListModelArray();
                for(int i=0;i<listModelArray.getSize();++i){
                    //出库uuid是sp_outflow表的逐渐,所以每出库一个listModelArray的元素就重新生成出库uuid
                    String uuid = UUID.randomUUID().toString();
                    Date date = new Date();
                    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String modtime = format.format(date);

                    Goods tmpgoods = listModelArray.getElementAt(i);
//                    System.out.println(tmpgoods.getGdBarCode());
                    goodsDao.OutFlow(uuid,tmpgoods.getGdBarCode(),tmpgoods.getGdNum(),modtime);

                    //出库操作完成，删除前一次的出库信息
                    OutFlowTextFields[0].setText("");
                    OutFlowTextFields[1].setText("");
                    OutFlowTextFields[2].setText("");

                    System.out.println(tmpgoods.getGdNum());
                }
                OutFlowList.Clear();
                MsgWin msgWin = new MsgWin("出库成功");
            }
        }
    }

    public static synchronized OutFlowUI getOutFlowUI() {
        if(OutFlowUI == null){
            OutFlowUI = new OutFlowUI();
        }
        return OutFlowUI;
    }

    public JPanel getOutFlowPanel(){
        return this.OutFlowPanel;
    }
    
    public void setScanningResults(String barcode){
        //接收扫码结果，更新信息
        OutFlowTextFields[0].setText(barcode);
        Goods goods = goodsDao.queryGoodsByBarCode(barcode);
        OutFlowTextFields[1].setText("1");
        OutFlowTextFields[2].setText(goods.getGdBroadName());
    }
}
